/***
 * * PROJECT
 * *    ATaG
 * * VERSION
 * *    $Revision: 1.6 $
 * * DATE
 * *    $Date: 2008/10/07 12:57:36 $
 * * LOG
 * *    $Log: Helper.java,v $
 * *    Revision 1.6  2008/10/07 12:57:36  animesh
 * *    removed GPL License [will re-add to all files later]
 * *
 * *    Revision 1.5  2007/08/19 07:31:00  animesh
 * *    file coping now happenning in 4KB chunks.. much faster.
 * *
 * *    Revision 1.4  2007/08/18 07:01:57  animesh
 * *    added fileCopy() and recursivelyCopyDirectory()
 * *
 * *    Revision 1.3  2007/01/15 09:12:45  animesh
 * *    no message
 * *
 * *    Revision 1.2  2006/10/19 21:21:53  lmottola
 * *    Added CVS preamble.
 * *
 * *
 * *   ATaG (Abstract Task Graph) - Data-driven Macroprogramming for
 * *                                Sensor Networks
 * *
 ***/

package atag.compiler;

import java.io.*;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2004</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class Helper {
    
    // Deletes all files and subdirectories under dir.
    // Returns true if all deletions were successful.
    // If a deletion fails, the method stops attempting to delete and returns false.
    public static boolean deleteAllFilesInDir(File dir) {
        if (dir.isDirectory()) {
            String[] children = dir.list();
            for (int i=0; i<children.length; i++) {
                boolean success = deleteAllFilesInDir(new File(dir, children[i]));
                if (!success) {
                    return false;
                }
            }
        } else {
            boolean success = dir.delete();
            if (!success)
                return false;
        }
        return true;
    }
    /** recursively copies all files and subdirectories under sourceDir to destDir
     * Returns true if all deletions were successful.
     * If a deletion fails, the method stops attempting to delete and returns false.
     */
    public static boolean recursivelyCopyDirectory(File sourceDir, File destDir)
    throws IOException{
        
        if(sourceDir.isDirectory() == false)
            return false;
        
        //do I need to do it?
        if(destDir.canRead() == true)
            if (deleteAllFilesInDir(destDir) == false)
                return false;
        
        
        String[] children = sourceDir.list();
        for (int i=0; i<children.length; i++) {
            File childSourceFile = new File(sourceDir, children[i]);
            File childDestFile = new File(destDir, children[i]);
            
            if(childSourceFile.isDirectory()){
                childDestFile.mkdir();
                
                if (recursivelyCopyDirectory(childSourceFile, childDestFile) == false)
                    return false;
                
            }else{
                //childSourceFile is a simple file.
                //create childDestFile as a file too.
                fileCopy(childSourceFile, childDestFile);
            }
        }// end for
        
        return true;
    }
    
    
    public static String[] splitFileIntoStrings(File fileName) {
        StringBuffer contents = new StringBuffer();
        String sContents;
        BufferedReader input = null;
        try {
            input = new BufferedReader(new FileReader(fileName));
            String line = null;
            while ( (line = input.readLine()) != null) {
                contents.append(line);
                contents.append(" ");
            }
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            try {
                if (input != null) {
                    input.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        sContents = contents.toString();
        return sContents.split(" ");
    }
    
    private static void fileCopy(File sourceFile, File destFile)
    throws IOException{
        FileOutputStream outStream = new FileOutputStream(destFile);
        FileInputStream inStream = new FileInputStream(sourceFile);
        
        int bytesread;
        byte[] buffer  = new byte[4096]; //4K chunks

        while((bytesread = inStream.read(buffer)) != -1)
            outStream.write(buffer, 0, bytesread);
        
        outStream.close();
        inStream.close();
    }
    
    /** for testing
     *
     */
    public static void main(String[] args){
        try {
            File sourceDir = new File(args[0]);
            
            File destDir = new File(args[1]);
            destDir.mkdir();
            
            recursivelyCopyDirectory(sourceDir, destDir);
            
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        
    }
    
}
